package com.zhaofei.myapplication;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.zhaofei.myapplication.Adapter.Adapter_ExMore;
import com.zhaofei.myapplication.Adapter.Adapter_Main;
import com.zhaofei.myapplication.Adapter.Adapter_More;
import com.zhaofei.myapplication.Bean.Bean_More;
import com.zhaofei.myapplication.Bean.Level0Item;
import com.zhaofei.myapplication.Bean.Level1Item;
import com.zhaofei.myapplication.Bean.Level3;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.onekeyshare.OnekeyShare;

public class MainActivity extends AppCompatActivity {


    @BindView(R.id.id_recyclerview)
    RecyclerView idRecyclerview;
    @BindView(R.id.id_button)
    Button idButton;
    private Adapter_Main adapterMain;
    private List<String> data = new ArrayList<>();
    private List<Bean_More> data2 = new ArrayList<>();
    private Adapter_More adapterMore;
    private Adapter_ExMore adapterExMore;
    private List<MultiItemEntity> datamult = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

//
        setIdRecyclerview();
//        setData2();
        idButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showShare();
            }
        });
        setzx();
    }

    private void setData2() {
        for (int i = 0; i < 15; i++) {
            if (i % 2 == 0) {
                data2.add(new Bean_More("模拟数据" + i, "111", 0));
            } else {
                data2.add(new Bean_More("模拟数据" + i, "222", 1));
            }

        }
        adapterMore.notifyDataSetChanged();
    }

    public void setData() {

        for (int i = 0; i < 15; i++) {
            data.add("模拟数据" + i);
        }
        adapterMain.notifyDataSetChanged();
    }

    public void setIdRecyclerview() {
        datamult = generateData();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        adapterExMore = new Adapter_ExMore(datamult, idRecyclerview, this);
//        final GridLayoutManager manager = new GridLayoutManager(this, 3);
//        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int position) {
//                return adapterExMore.getItemViewType(position) != Adapter_ExMore.TYPE_LEVEL_0 ? 1 : manager.getSpanCount();
//            }
//        });

//        adapterMain = new Adapter_Main(data, this);
//        idRecyclerview.setAdapter(adapterMain);
//        adapterMore = new Adapter_More(data2);


        idRecyclerview.setAdapter(adapterExMore);
        idRecyclerview.setLayoutManager(linearLayoutManager);


//        adapterExMore.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ImageView imageView = (ImageView) adapter.getViewByPosition(idRecyclerview, position, R.id.id_item_image);

//
////                adapter.notifyItemChanged(position);
////                List<Level0Item> list = adapter.getData();
////                Level0Item lv0 = list.get(position);
////                int pos = position;
//                Toast.makeText(MainActivity.this, ""+position, Toast.LENGTH_SHORT).show();
////                if (lv0.isExpanded()) {
////                    adapter.collapse(pos, true, true);
////                } else {
////                    adapter.expand(pos, false, true);
////                }
//            }
//        });
    }

    private void setzx() {
        adapterExMore.setOnAddClickListener(new Adapter_ExMore.OnAddClickListener() {
            @Override
            public void onItemClick(int position, boolean add) {
                ImageView imageView = (ImageView) adapterExMore.getViewByPosition(idRecyclerview, position, R.id.id_item_image);
                if (add) {
                    Glide.with(MainActivity.this).load(R.drawable.up).into(imageView);
                    adapterExMore.notifyItemChanged(position);
                } else {
                    Glide.with(MainActivity.this).load(R.drawable.down).into(imageView);
                    adapterExMore.notifyItemChanged(position);
                }
            }
        });
    }

    private ArrayList<MultiItemEntity> generateData() {
        int lv0Count = 20;
        int lv1Count = 3;
        int L3 = 5;

        ArrayList<MultiItemEntity> res = new ArrayList<>();
        for (int i = 0; i < lv0Count; i++) {
            Level0Item lv0 = new Level0Item("This is " + i + "th item in Level 0", "subtitle of " + i);
            for (int j = 0; j < lv1Count; j++) {
                Level1Item lv1 = new Level1Item("父级:" + i + "Level 1 item: " + j, "(no animation)");
                for (int i1 = 0; i1 < L3; i1++) {
                    Level3 level3 = new Level3("父级" + j + "第三级数据" + i1);
                    lv1.addSubItem(level3);
                }


                lv0.addSubItem(lv1);
            }
            res.add(lv0);
        }
//        res.add(new  Level0Item("This is " + lv0Count + "th item in Level 0", "subtitle of " + lv0Count));
        return res;
    }

    public void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(getString(R.string.app_name));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        oks.setImageUrl("http://img3.imgtn.bdimg.com/it/u=4109991441,132882816&fm=200&gp=0.jpg");
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(this);
    }
}
